/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HealthMonitoringView;

import Common.Constant;
import DataSource.DataEntry;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/**
 *
 * @author Tuan
 */
public class DataEntryForm extends AbstractForm {

    private TextField txtMinSBP;
    private TextField txtMaxSBP;
    private TextField txtMinDBP;
    private TextField txtMaxDBP;
    private TextField txtMinHeartBeat;
    private TextField txtMaxHeartBeat;
    private ChoiceGroup chGrp;    
    private Command okCommand;    
    private int minSBP;
    private int maxSBP;
    private int minDBP;
    private int maxDBP;
    private int minHeartBeat;
    private int maxHeartBeat;
    private boolean isHypotension;
    private boolean isHypertensionStage1;
    private boolean isHypertensionStage2;
    private boolean isHypertensionStage3;
    private boolean isHeartStroke;
    private boolean isHeathy;
    
    private StringItem lblMinSbpErr;
    private StringItem lblMaxSbpErr;
    private StringItem lblMinDbpErr;
    private StringItem lblMaxDbpErr;
    private StringItem lblMinHbErr;
    private StringItem lblMaxHbErr;
    

    public DataEntryForm(MonitoringMIDlet parent) {
        super("Data Entry", null, parent);       
    }

    protected void initialize() {
        super.initialize();
        
        lblMinSbpErr = new StringItem("", "");
        lblMaxSbpErr = new StringItem("", "");
        lblMinDbpErr = new StringItem("", "");
        lblMaxDbpErr = new StringItem("", "");
        lblMinHbErr = new StringItem("", "");
        lblMaxHbErr = new StringItem("", "");
        
        setTxtMinSBP(new TextField("Min SBP", null, 32, TextField.NUMERIC));
        setTxtMaxSBP(new TextField("Max SBP", null, 32, TextField.NUMERIC));
        setTxtMinDBP(new TextField("Min DBP", null, 32, TextField.NUMERIC));
        setTxtMaxDBP(new TextField("Max DBP", null, 32, TextField.NUMERIC));
        setTxtMinHeartBeat(new TextField("Min Heart Beat", null, 32, TextField.NUMERIC));
        setTxtMaxHeartBeart(new TextField("Max Heart Beat", null, 32, TextField.NUMERIC));
        setChGrp(new ChoiceGroup("Rules", Choice.MULTIPLE));
        getChGrp().append("Hypotension", null);
        getChGrp().append("Healthy Condition", null);
        getChGrp().append("Hypertension Stage 1", null);
        getChGrp().append("Hypertension Stage 2", null);
        getChGrp().append("Hypertension Stage 3", null);
        getChGrp().append("Heart Stoke", null);
        getChGrp().setSelectedFlags(new boolean[]{true, true, true, true, true, true});
        this.append(getTxtMinSBP());
        this.append(lblMinSbpErr);
        this.append(getTxtMaxSBP());
        this.append(lblMaxSbpErr);
        this.append(getTxtMinDBP());
        this.append(lblMinDbpErr);
        this.append(getTxtMaxDBP());
        this.append(lblMaxDbpErr);
        this.append(getTxtMinHeartBeat());
        this.append(lblMinHbErr);
        this.append(getTxtMaxHeartBeart());
        this.append(lblMaxHbErr);
        this.append(getChGrp());        
        this.addCommand(getOkCommand());

        this.txtMinSBP.setString("90");
        this.txtMaxSBP.setString("139");
        this.txtMinDBP.setString("60");
        this.txtMaxDBP.setString("89");
        this.txtMinHeartBeat.setString("60");
        this.txtMaxHeartBeat.setString("100");

    }

    public void commandAction(Command c, Displayable d) {
        if (c == getOkCommand()) {
            int minSBP = Integer.parseInt(getTxtMinSBP().getString());
            int maxSBP = Integer.parseInt(getTxtMaxSBP().getString());
            int minDBP = Integer.parseInt(getTxtMinDBP().getString());
            int maxDBP = Integer.parseInt(getTxtMaxDBP().getString());
            int minHeartBeat = Integer.parseInt(getTxtMinHeartBeat().getString());
            int maxHeartBeat = Integer.parseInt(getTxtMaxHeartBeart().getString());
            
            lblMinSbpErr.setText("");
            lblMaxSbpErr.setText("");
            lblMinDbpErr.setText("");
            lblMaxDbpErr.setText("");
            lblMinHbErr.setText("");
            lblMaxHbErr.setText("");
            StringBuffer builder = new StringBuffer();
            if(minSBP <= Constant.HYPOTENSION_MAX_SBP){
                String error = "Min SBP must larger than " + Constant.HYPOTENSION_MAX_SBP + "\n";
                builder.append(error);
                lblMinSbpErr.setText(error);                
            }
            
            if(maxSBP <= minSBP) {
                String error = "Max SBP must larger than Min SBP\n";
                builder.append("Max SBP must larger than Min SBP").append("\n");
                lblMaxSbpErr.setText(error);
            }
            
            if(maxSBP >= Constant.HYPERTENSION_MIN_SBP_STAGE1) {
                String error = "Max SBP must smaller than " + Constant.HYPERTENSION_MIN_SBP_STAGE1 + "\n";
                builder.append("Max SBP must smaller than ").append(Constant.HYPERTENSION_MIN_SBP_STAGE1).append("\n");
                lblMaxSbpErr.setText(lblMaxDbpErr.getText() + error);
            }
            
            if(minDBP <= Constant.HYPOTENSION_MAX_DBP) {
                String error = "Min DBP must larger than " + Constant.HYPOTENSION_MAX_DBP + "\n";
                builder.append(error);
                lblMinDbpErr.setText(error);
            }
            
            if(maxDBP <= minDBP) {
                String error = "Max DBP must larger than Min DBP\n";
                builder.append("Max DBP must larger than Min DBP").append("\n");
                lblMaxDbpErr.setText(error);
            }
            
            if(maxDBP >= Constant.HYPERTENSION_MIN_DBP_STAGE1) {
                String error = "Max DBP must smaller than " + Constant.HYPERTENSION_MIN_DBP_STAGE1 + "\n";
                builder.append("Max DBP must smaller than ").append(Constant.HYPERTENSION_MIN_DBP_STAGE1).append("\n");
                lblMaxDbpErr.setText(lblMaxDbpErr.getText() + error);
            }
            
            if(minHeartBeat < Constant.MIN_HEART_BEAT){
                String error = "Min Heart Beat must larger than " + Constant.MIN_HEART_BEAT + "\n";
                builder.append(error);
                lblMinHbErr.setText(error);
            }
            
            if(maxHeartBeat <= minHeartBeat) {
                String error = "Max Heart Beat must larger than Min Heart Beat\n";
                builder.append("Max Heart Beat must larger than Min Heart Beat").append("\n");
                lblMaxHbErr.setText(error);
            }
            
            if(maxHeartBeat > Constant.MAX_HEART_BEAT) {
                String error = "Max Heart Beat must smaller than " + Constant.MAX_HEART_BEAT + "\n";
                builder.append("Max Heart Beat must smaller than ").append(Constant.MAX_HEART_BEAT).append("\n");
                lblMaxHbErr.setText(lblMaxHbErr.getText() + error);
            }
            
            boolean[] selectedFlags = new boolean[getChGrp().size()];
            this.getChGrp().getSelectedFlags(selectedFlags);
            int selectedCount = selectedFlags.length;
            for(int i = 0; i < selectedFlags.length; i++){
                if(selectedFlags[i] == false){
                    selectedCount--;                    
                }
            }
            if(selectedCount == 0){
                builder.append("Please choose at least one health condition\n");
            }
            
            if(builder.length() > 0){
                Alert alert = new Alert("Data is invalid", builder.toString(), null, AlertType.ERROR);
                parent.showScreen(alert);
                return;
            }
            
            
            DataEntry entry = new DataEntry();
            entry.setMinSBP(minSBP);
            entry.setMaxSBP(maxSBP);
            entry.setMinDBP(Integer.parseInt(getTxtMinDBP().getString()));
            entry.setMaxDBP(Integer.parseInt(getTxtMaxDBP().getString()));
            entry.setMinHeartBeat(Integer.parseInt(getTxtMinHeartBeat().getString()));
            entry.setMaxHeartBeat(Integer.parseInt(getTxtMaxHeartBeart().getString()));
            
            entry.setIsHypotension(selectedFlags[0]);
            entry.setIsHeathy(selectedFlags[1]);
            entry.setIsHypertensionStage1(selectedFlags[2]);
            entry.setIsHypertensionStage2(selectedFlags[3]);
            entry.setIsHypertensionStage3(selectedFlags[4]);
            entry.setIsHeartStroke(selectedFlags[5]);
            parent.dataEntered(entry);
            return;
        }
        super.commandAction(c, d);
    }    

    public Command getOkCommand() {
        if (okCommand == null) {
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 6);
            // write post-init user code here
        }
        return okCommand;
    }

    /**
     * @return the txtMinSBP
     */
    public TextField getTxtMinSBP() {
        return txtMinSBP;
    }

    /**
     * @param txtMinSBP the txtMinSBP to set
     */
    public void setTxtMinSBP(TextField txtMinSBP) {
        this.txtMinSBP = txtMinSBP;
    }

    /**
     * @return the txtMaxSBP
     */
    public TextField getTxtMaxSBP() {
        return txtMaxSBP;
    }

    /**
     * @param txtMaxSBP the txtMaxSBP to set
     */
    public void setTxtMaxSBP(TextField txtMaxSBP) {
        this.txtMaxSBP = txtMaxSBP;
    }

    /**
     * @return the txtMinDBP
     */
    public TextField getTxtMinDBP() {
        return txtMinDBP;
    }

    /**
     * @param txtMinDBP the txtMinDBP to set
     */
    public void setTxtMinDBP(TextField txtMinDBP) {
        this.txtMinDBP = txtMinDBP;
    }

    /**
     * @return the txtMaxDBP
     */
    public TextField getTxtMaxDBP() {
        return txtMaxDBP;
    }

    /**
     * @param txtMaxDBP the txtMaxDBP to set
     */
    public void setTxtMaxDBP(TextField txtMaxDBP) {
        this.txtMaxDBP = txtMaxDBP;
    }

    /**
     * @return the txtMinHeartBeat
     */
    public TextField getTxtMinHeartBeat() {
        return txtMinHeartBeat;
    }

    /**
     * @param txtMinHeartBeat the txtMinHeartBeat to set
     */
    public void setTxtMinHeartBeat(TextField txtMinHeartBeat) {
        this.txtMinHeartBeat = txtMinHeartBeat;
    }

    /**
     * @return the txtMaxHeartBeart
     */
    public TextField getTxtMaxHeartBeart() {
        return txtMaxHeartBeat;
    }

    /**
     * @param txtMaxHeartBeart the txtMaxHeartBeart to set
     */
    public void setTxtMaxHeartBeart(TextField txtMaxHeartBeart) {
        this.txtMaxHeartBeat = txtMaxHeartBeart;
    }

    /**
     * @return the chGrp
     */
    public ChoiceGroup getChGrp() {
        return chGrp;
    }

    /**
     * @param chGrp the chGrp to set
     */
    public void setChGrp(ChoiceGroup chGrp) {
        this.chGrp = chGrp;
    }

    /**
     * @param backCommand the backCommand to set
     */
    public void setBackCommand(Command backCommand) {
        this.backCommand = backCommand;
    }

    /**
     * @param okCommand the okCommand to set
     */
    public void setOkCommand(Command okCommand) {
        this.okCommand = okCommand;
    }

    /**
     * @return the minSBP
     */
    public int getMinSBP() {
        return minSBP;
    }

    /**
     * @param minSBP the minSBP to set
     */
    public void setMinSBP(int minSBP) {
        this.minSBP = minSBP;
    }

    /**
     * @return the maxSBP
     */
    public int getMaxSBP() {
        return maxSBP;
    }

    /**
     * @param maxSBP the maxSBP to set
     */
    public void setMaxSBP(int maxSBP) {
        this.maxSBP = maxSBP;
    }

    /**
     * @return the minDBP
     */
    public int getMinDBP() {
        return minDBP;
    }

    /**
     * @param minDBP the minDBP to set
     */
    public void setMinDBP(int minDBP) {
        this.minDBP = minDBP;
    }

    /**
     * @return the maxDBP
     */
    public int getMaxDBP() {
        return maxDBP;
    }

    /**
     * @param maxDBP the maxDBP to set
     */
    public void setMaxDBP(int maxDBP) {
        this.maxDBP = maxDBP;
    }

    /**
     * @return the minHeartBeat
     */
    public int getMinHeartBeat() {
        return minHeartBeat;
    }

    /**
     * @param minHeartBeat the minHeartBeat to set
     */
    public void setMinHeartBeat(int minHeartBeat) {
        this.minHeartBeat = minHeartBeat;
    }

    /**
     * @return the maxHeartBeat
     */
    public int getMaxHeartBeat() {
        return maxHeartBeat;
    }

    /**
     * @return the isHypotension
     */
    public boolean isHypotension() {
        return isHypotension;
    }

    /**
     * @return the isHypertensionStage1
     */
    public boolean isHypertensionStage1() {
        return isHypertensionStage1;
    }

    /**
     * @return the isHypertensionStage2
     */
    public boolean isHypertensionStage2() {
        return isHypertensionStage2;
    }

    /**
     * @return the isHypertensionStage3
     */
    public boolean isHypertensionStage3() {
        return isHypertensionStage3;
    }

    /**
     * @return the isHeartStroke
     */
    public boolean isHeartStroke() {
        return isHeartStroke;
    }

    /**
     * @return the isHeathy
     */
    public boolean isHeathy() {
        return isHeathy;
    }
}
